#!/bin/sh -efu
#
# Copyright (c) 2017 Dmitry V. Levin <ldv@strace.io>
# Copyright (c) 2017-2018 The strace developers.
# Copyright (c) 2021 Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

usage()
{
	cat >&2 <<EOF
Usage: $0 [<input> [<output>]]

Generate test script(s) from <input> table.
If a <output> is specified, generate the script for this test only,
otherwise generate scripts for all tests specified in <input> table.
EOF
	exit 1
}

input="${0%/*}/gen_tests.in"
dir="$(dirname "$input")"

[ $# -eq 0 ] || { input="$1"; shift; }
output=
[ $# -eq 0 ] || { output="$1"; shift; }
[ $# -eq 0 ] || usage

match=
if [ -n "$output" ]; then
	match="${output##*/}"
	match="${match%.gen.test}"
	[ -n "$match" ] || usage
fi

names=
gen_test() {
	local name="$1"; shift
	local args="$1"; shift
	local subname="${name#*-}"

	local kind field1 field2 dummy
	IFS='-' read -r kind field1 field2 dummy <<-EOF
	$name
	EOF

	if [ -z "$match" ]; then
		names="$names $name"
		output="$dir/$name.gen.test"
	else
		[ "$match" = "$name" ] || return 0
	fi

	hdr="\
#!/bin/sh -efu
# Generated by $0 from $input ($name $args); do not edit."

	case "$kind" in
		heavy_girar_patch|patch_repo_inplace)
			[ -n "$field1" ]
			[ -n "$field2" ]
			cat <<-EOF
			$hdr
			. "\${srcdir=.}/$kind".sh "$field1" "$field2" $args
			EOF
			;;
		fail_patch_repo_inplace)
			[ -n "$field1" ]
			[ -n "$field2" ]
			cat <<-EOF
			$hdr
			export genbasedir_should_fail=1
			. "\${srcdir=.}/patch_repo_inplace".sh "$field1" "$field2" $args
			EOF
			;;
		heavy_girar_regen|regen_repo)
			[ -n "$field1" ]
			cat <<-EOF
			$hdr
			. "\${srcdir=.}/$kind".sh "$field1" $args
			EOF
			;;
		sleep)
			[ -n "$subname" ]
			cat <<-EOF
			$hdr
			builddir="\$(realpath ..)"
			export GENBASEDIR="\$builddir"/tests/genbasedir_sleep

			. "\${srcdir=.}/$subname".gen.test
			EOF
			;;
		input_compression_*)
			[ -n "$subname" ]
			local comp="${kind#input_compression_}"
			cat <<-EOF
			$hdr
			export keep_compressed=$comp

			. "\${srcdir=.}/$subname".gen.test
			EOF
			;;
		invalidate)
			[ -n "$subname" ]
			local comp="${kind#input_compression_}"
			cat <<-EOF
			$hdr
			builddir="\$(realpath ..)"
			export GENBASEDIR="\$builddir"/tests/genbasedir_invalidate
			export keep_compressed=xz

			. "\${srcdir=.}/$subname".gen.test
			EOF
			;;
		spurious_old)
			[ -n "$subname" ]
			local comp="${kind#input_compression_}"
			cat <<-EOF
			$hdr
			builddir="\$(realpath ..)"
			export GENBASEDIR="\$builddir"/tests/genbasedir_invalidate
			export keep_compressed=xz
			export add_spurious_old=1

			. "\${srcdir=.}/$subname".gen.test
			EOF
			;;
		*)
			echo "generation of $name test is not supported" >&2
			exit 1
		;;
	esac > "$output"

	chmod a+x "$output"
}

while read -r name args; do {
	[ -n "${name###*}" ] || continue

	gen_test "$name" "$args"
	case "$name" in
		heavy_girar_*) ;;
		patch_repo_inplace-*)
			gen_test "input_compression_xz-$name" ''
			gen_test "input_compression_none-$name" ''
			gen_test "input_compression_bz2-$name" ''
			gen_test invalidate-"$name" ''
			gen_test spurious_old-"$name" ''
			;;
		*)
			gen_test sleep-"$name" ''
			;;
	esac

} < /dev/null; done < "$input"

if [ -n "$names" ]; then
	{
		printf '# Generated by %s from %s; do not edit.\n' "$0" "$input"
		printf 'GEN_TESTS ='
		printf ' %s.gen.test' $names
		echo
		target='$(srcdir)/%s.gen.test'
		dep1='$(abs_srcdir)/gen_tests.sh'
		dep2='$(srcdir)/gen_tests.in'
		recipe='$(AM_V_GEN) $^ $@'
		printf "\\n$target: $dep1 $dep2\\n\\t$recipe\\n" $names
	} > "$dir/gen_tests.am"
fi
